package com.cyandpos.pad.pay.model;

import android.text.TextUtils;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.CardCouponItem;
import com.cyandpos.pad.posdata.Coupon;
import com.cyandpos.pad.posdata.CouponPayLimit;
import com.cyandpos.pad.posdata.GetVipCouponResponse;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.utils.MoneyUtils;
import com.google.gson.JsonObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2020/1/16.
 */

public class IPayCardCouponModelImpl implements IPayCardCouponModel {

    /**
     * 卡券查询
     * @param data
     * @param listener
     */
    @Override
    public void loadYhqkInfo(QueryMemberRequest data, final YhqkRequestListener listener) {
        //向网络发请求
        PosHttpManager.QueryMemberCouponExcute(data,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject res) {

                BaseResponse<GetVipCouponResponse> baseResponse = BaseResponse.fromJson(res.toString(),GetVipCouponResponse.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        GetVipCouponResponse data = baseResponse.getData();
                        if (data.getCouponList() != null && data.getCouponList().size() > 0 ){
                            if(data.getPayLimitList()!=null && data.getPayLimitList().size() >0){
                                ArrayList<CardCouponItem> availableList = new ArrayList<>();
                                for (Coupon item : data.getCouponList()) {
                                    CardCouponItem cardItem = new CardCouponItem();
                                    cardItem.setCardId(data.getVipId());
                                    cardItem.setCardCode(data.getVipCode());
                                    cardItem.setCouponType(item.getCouponType());
                                    cardItem.setCouponTypeName(item.getCouponTypeName());
                                    cardItem.setBalance( MoneyUtils.Double2Int(item.getBalance()));
                                    for (CouponPayLimit limitItem : data.getPayLimitList()) {
                                        if (item.getCouponType() == limitItem.getCouponType()) {
                                            cardItem.setLimitMoney( MoneyUtils.Double2Int(limitItem.getLimitMoney() ));
                                            boolean cardCouponIsAvailable = false;
                                            for (Payment e : ErpCommonData.loadConfig.getPayList()) {
                                                if (e.getYhqId() == item.getCouponType() && e.getIsCoded()==0) {//卡券
                                                    cardCouponIsAvailable = true;
                                                    break;
                                                }
                                            }
                                            if (cardItem.getLimitMoney() != 0 && cardItem.getBalance() != 0 && cardCouponIsAvailable) {
                                                availableList.add(cardItem);
                                            }
                                            break;
                                        }
                                    }
                                }
                                listener.getyhqkInfoSuccess(availableList);

                            }else{
                                listener.getyhqkInfoFailure("无可用卡券信息");
                            }
                        }
                        else{
                            listener.getyhqkInfoFailure("无可用卡券信息");
                        }
                    }catch (Exception e){
                        listener.getyhqkInfoFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.getyhqkInfoFailure(baseResponse.getMsg());
                    }else {
                        listener.getyhqkInfoFailure(baseResponse.getTips());
                    }
                }

            }
            @Override
            public void onFailure(String msg) {
                listener.getyhqkInfoFailure(msg);
            }
        });
    }


}
